﻿using System;
using System.Drawing;
using System.Threading;
using SinaWeiboPlugin.Properties;
using Slippor.Common.PluginFramework;
using Slippor.Douban.Radio.Assist;

namespace SinaWeiboPlugin
{
    class TweetBuilder
    {
        private static SinaTHelper _helper = new SinaTHelper();
        private Settings _settings = SinaWeiboConfig.SettingManager.Settings as Settings;
        private object _forLock = new object();

        public TweetBuilder(SinaWeiboSharer handler)
        {
            _handler = handler;
        }

        private String _message = String.Empty;
        public String Message
        {
            get { return _message; }
            set { _message = value; }
        }

        private Image _snap;
        public Image Snap
        {
            get { return _snap; }
        }

        private SinaWeiboSharer _handler;
        public SinaWeiboSharer Handler
        {
            get { return _handler; }
        }

        public IApplication App
        {
            get { return _handler.Plugin.Application; }
        }

        private readonly Rectangle _coverRect = new Rectangle(28, 34, 120, 120);
        public void Refresh()
        {
            lock (_forLock)
            {
                if (_handler.CurrentSong != null)
                {
                    _message = GetName(_settings.DefaultString, _handler.CurrentSong);
                    if (_settings.SnapRegion == "Cover")
                    {
                        _snap = Utility.DrawToImage(App.MainPanel, _coverRect);
                    }
                    else
                    {
                        _snap = Utility.DrawToImage(App.MainPanel);
                    }
                }
            }
        }

        public bool Upload()
        {
            lock (_forLock)
            {
                int tryTime = 3;
                bool result = false;
                while (true)
                {
                    try
                    {
                        if (_settings.WithSnapShot)
                        {
                            result = _helper.Upload(this);
                        }
                        else
                        {
                            result = _helper.Update(this);
                        }
                        break;
                    }
                    catch (Exception e)
                    {
                        tryTime--;
                        if (tryTime > 0)
                        {
                            Thread.Sleep(1000);
                        }
                        else
                        {
                            throw e;
                        }
                    }
                }
                return result;
            }
        }

        internal String UserName
        {
            get { return _settings.UserName; }
        }

        internal String Password
        {
            get { return _settings.Password; }
        }

        public const String TitleMark = "#song";
        public const String ArtistMark = "#artists";
        public const String AlbumMark = "#album";
        public const String LabelMark = "#label";
        public const String UrlMark = "#url";
        private string GetName(String nameFormate, Song song)
        {
            if(song.Album != null)
            {
                return nameFormate.Replace(TitleMark, song.Title).Replace(AlbumMark, song.Album.Name).Replace(ArtistMark, song.Album.ArtistString).Replace(UrlMark, String.Format("http://music.douban.com/subject/{0}/",song.Album.AlbumID)).Replace(LabelMark, "#豆瓣电台#");
            }
            else
            {
                return nameFormate.Replace(TitleMark, song.Title).Replace(AlbumMark, String.Empty).Replace(ArtistMark, String.Empty).Replace(UrlMark, String.Empty).Replace(LabelMark, "#豆瓣电台#");
            }
        }
    }
}
